package com.ai.enviro.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * mysql 代码生成器演示例子
 * </p>
 *
 * @author huhaichao
 */
public class CodeGenerator {

    private static String projectPath;

    /**
     * RUN THIS
     */
    public static void generate(GeneratorConfig c) {
        String parentPacakge = c.getParentPacakge();

        String dataSource = c.getDataSource();

        String[] tables = c.getTables();
        String[] tablePrefix = c.getTablePrefix();

        String jdbcUrl = c.getJdbcUrl();
        String name = c.getName();
        String driver = c.getDriver();
        String passwd = c.getPasswd();

        String author = c.getAuthor();
        String module = c.getModule();


        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        projectPath = System.getProperty("user.dir");
        //gc.setOutputDir(projectPath + "/wec-cpdaily-authority-dal/src/main/java");
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setFileOverride(true);
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        gc.setEntityName("%s" + "DO");
        gc.setServiceName("%s" + "Dao");
        gc.setServiceImplName("%s" + "DaoImpl");
        mpg.setGlobalConfig(gc);


        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(jdbcUrl);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driver);
        dsc.setUsername(name);
        dsc.setPassword(passwd);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("dal");
        pc.setParent(parentPacakge);

        pc.setController("module." + module + ".controller");
        pc.setService("module." + module + ".dao.mysql");
        pc.setServiceImpl("module." + module + ".dao.mysql.impl");
        pc.setEntity("module." + module + ".dao.dataobject");

        //多数据源暂未修改，mapper暂放外面
        pc.setMapper("mapper." + dataSource);
        pc.setXml("mapper." + dataSource);


        Map<String, String> pathInfo = new HashMap<>(6);
        pathInfo.put("controller_path", buildPath("/gen/src/main/java/", pc.getParent() + "." + pc.getController()));

        pathInfo.put("service_path", buildPath("/gen/src/main/java/", pc.getParent() + "." + pc.getService()));
        pathInfo.put("service_impl_path", buildPath("/gen/src/main/java/", pc.getParent() + "." + pc.getServiceImpl()));

        pathInfo.put("mapper_path", buildPath("/gen/src/main/java/", pc.getParent() + "." + pc.getMapper()));
        pathInfo.put("entity_path", buildPath("/gen/src/main/java/", pc.getParent() + "." + pc.getEntity()));

        pathInfo.put("xml_path", buildPath("/gen/src/main/resources/", pc.getMapper()));


        pc.setPathInfo(pathInfo);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig());

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setSuperControllerClass("com.ai.enviro.common.BaseController");
        strategy.setInclude(tables);
        //strategy.setSuperEntityColumns("id");
        //strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityColumnConstant(true);
        strategy.setRestControllerStyle(true);


        strategy.setTablePrefix(tablePrefix);

        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    private static String buildPath(String modelPath, String pack) {
        return projectPath + modelPath + pack.replace(".", "/");
    }

}
